package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.pojo.PageQuery;
import com.cf.domain.vo.TeamVO;
import com.cf.domain.vo.UserVO;
import com.cf.enums.Area;
import com.cf.enums.UserRole;
import com.cf.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequiredArgsConstructor
public class IndexController {

    private final IUserService iUserService;

    @GetMapping("/main")
    public String main(@RequestParam(name = "areaSearch", required = false) String areaSearch,
                       @RequestParam(name = "pageNo", required = false) String pageNo,
                       HttpSession session, Model model) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        Area[] areas = Area.values();
        model.addAttribute("areas", areas);
        if(userVO.getUserRole() == 1 || userVO.getUserRole() == 2){
            if(pageNo == null){
                pageNo = "1";
            }
            PageQuery pageQuery = new PageQuery(Integer.valueOf(pageNo), 10);
            model.addAttribute("pageNo", pageNo);
            if(userVO.getUserRole() == UserRole.ADMIN.getId()){
                //如果是管理员
                if(areaSearch == null){
                    areaSearch = "0";
                }
                pageQuery.getQueryMap().put("areaSearch", areaSearch);
                model.addAttribute("areaSearch", areaSearch);
            }else if(userVO.getUserRole() == UserRole.REGIONADMIN.getId()){
                //如果是地域管理员
                pageQuery.getQueryMap().put("areaSearch", String.valueOf(userVO.getArea()));
            }
            Page<TeamVO> data = iUserService.selectTeamList(pageQuery, userVO);
            model.addAttribute("data", data);
            return "admin/main";
        }else{
            return "user/main";
        }

    }
}
